﻿#region File Description
/****************************************************************************
*                                                                           *
* Gameplay Screen.                                                          *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2010).                  *
*                                                                           *
****************************************************************************/
#endregion

#region Using Statements
using System;
using System.Threading;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace XNA_FRAMEWORK
{
	class XNACustomScreen : XNAGameScreen
	{
		#region Fields

		ContentManager content;
		GraphicsDevice device;
		GameModel pyramidModel = new GameModel();
		Effect effect;
		VertexPositionColor[] vertices;
		VertexDeclaration myVertexDeclaration;
		Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
		Vector3 cameraLookAt = new Vector3(0.0f, 50.0f, 0.0f);
		Matrix cameraProjectionMatrix;
		Matrix cameraViewMatrix;


		#endregion

		#region Initialization

		public XNACustomScreen()
		{
			TransitionOnTime = TimeSpan.FromSeconds(1.5);
			TransitionOffTime = TimeSpan.FromSeconds(0.5);
		}

		private void SetUpVertices()
		{
			vertices = new VertexPositionColor[3];

			vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
			vertices[0].Color = Color.Red;
			vertices[1].Position = new Vector3(0, 0.5f, 0f);
			vertices[1].Color = Color.Green;
			vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
			vertices[2].Color = Color.Yellow;

			myVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
		}

		public override void LoadContent()
		{
			if (content == null)
				content = new ContentManager(ScreenManager.Game.Services, "Content");

			device = ScreenManager.GraphicsDevice;
			effect = content.Load<Effect>("effects");

			cameraViewMatrix = Matrix.CreateLookAt(
				cameraPosition,
				cameraLookAt,
				Vector3.Up);

			cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
				MathHelper.ToRadians(45.0f),
				ScreenManager.GraphicsDevice.Viewport.AspectRatio,
				1.0f,
				10000.0f);

			pyramidModel.model = content.Load<Model>(
				"Models\\horse");
			
			SetUpVertices();

			Thread.Sleep(1000);
			ScreenManager.Game.ResetElapsedTime();
		}

		public override void UnloadContent()
		{
			content.Unload();
		}

		#endregion

		#region Update and Draw

		public override void Update(GameTime gameTime, bool otherScreenHasFocus,
													   bool coveredByOtherScreen)
		{
			if (IsActive)
			{		
				KeyboardState keyboardState = Keyboard.GetState();
				MouseState mouseState = Mouse.GetState();

				if (keyboardState.IsKeyDown(Keys.Left))
				{
					pyramidModel.position.X -= 0.05f;
				}
				if (keyboardState.IsKeyDown(Keys.Right))
				{
					pyramidModel.position.X += 0.05f;
				}
				if (keyboardState.IsKeyDown(Keys.Up))
				{
					pyramidModel.position.Y += 0.05f;
				}
				if (keyboardState.IsKeyDown(Keys.Down))
				{
					pyramidModel.position.Y -= 0.05f;
				}

				if (keyboardState.IsKeyDown(Keys.Q))
				{
					pyramidModel.rotation.X += 0.05f;
				}
				if (keyboardState.IsKeyDown(Keys.W))
				{
					pyramidModel.rotation.Y -= 0.05f;
				}
				if (ButtonState.Pressed == mouseState.LeftButton)
					pyramidModel.position.X += 0.05f;
			}

			base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
		}

		public override void HandleInput(XNAInput input)
		{
			if (input == null)
				throw new ArgumentNullException("input");

			int playerIndex = (int)ControllingPlayer.Value;

			KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
			GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];

			bool gamePadDisconnected = !gamePadState.IsConnected &&
									   input.GamePadWasConnected[playerIndex];

			if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
			{
				ScreenManager.AddScreen(new XNAPauseMenuScreen(), ControllingPlayer);
			}
		}

		public override void Draw(GameTime gameTime)
		{
			ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
											   Color.CornflowerBlue, 0, 0);

			DrawGameObjectX(pyramidModel);

			effect.CurrentTechnique = effect.Techniques["Pretransformed"];
			effect.Begin();
			foreach (EffectPass pass in effect.CurrentTechnique.Passes)
			{
				pass.Begin();

				device.VertexDeclaration = myVertexDeclaration;
				device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);

				pass.End();
			}
			effect.End();

			base.Draw(gameTime);	
		}

		void DrawGameObjectX(GameModel pyramidModel)
		{
			foreach (ModelMesh mesh in pyramidModel.model.Meshes)
			{
				foreach (BasicEffect effect in mesh.Effects)
				{
					effect.EnableDefaultLighting();
					effect.PreferPerPixelLighting = true;

					effect.World =
						Matrix.CreateFromYawPitchRoll(
						pyramidModel.rotation.Y,
						pyramidModel.rotation.X,
						pyramidModel.rotation.Z) *

						Matrix.CreateScale(pyramidModel.scale) *

						Matrix.CreateTranslation(pyramidModel.position);

					effect.Projection = cameraProjectionMatrix;
					effect.View = cameraViewMatrix;
				}
				mesh.Draw();
			}
		}
		#endregion

	}

	class GameModel
	{
		public Model model = null;
		public Vector3 position = Vector3.Zero;
		public Vector3 rotation = Vector3.Zero;
		public float scale = 1.0f;
		public Vector3 velocity = Vector3.Zero;
		public bool alive = false;
	}
}
